package com.sample.kokou;

import java.sql.SQLException;
import java.util.List;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;

import android.os.Bundle;
import android.widget.TextView;

public class OrmLiteSampleActivity extends OrmLiteBaseActivity<DatabaseHelper> {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView v = GenericUtil.getView(this, R.id.displayText);
        display(v);
    }

    private void display(TextView v) {
        try {
            Dao<Person, Integer> dao = getHelper().getDao(Person.class);
            List<Person> people = dao.queryForAll();
            StringBuilder sb = new StringBuilder();
            for (Person person : people) {
                sb.append(person.getName());
                sb.append("\t");
                sb.append(String.format("%02d\n", person.getAge()));
            }
            v.setText(sb.toString());
        } catch (SQLException e) {
            v.setText("Fail.");
        }
    }
}